<%
# encoding: utf-8
#
# Redmine plugin for Document Management System "Features"
#
# Copyright © 2011-20 Karel Pičman <karel.picman@kontron.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
%>

<h3 class="title"><%= l(:title_dmsf_workflow_log) %></h3>
<p>
  <% if params[:dmsf_file_revision_id].present? %>
    <% revision = DmsfFileRevision.find_by(id: params[:dmsf_file_revision_id]) %>
      <% if revision %>
        <div class="dmsf_log_header_box">                    
          <div class="dmsf_log_header_left">
            <%= label_tag 'workflow_name', "#{l(:link_workflow)} #{l(:field_name).downcase} " %>
            <% if User.current.allowed_to?(:manage_workflows, @dmsf_workflow.project) %>
              <%= link_to @dmsf_workflow.name, dmsf_workflow_path(@dmsf_workflow) %>
            <% else %>
              <%= @dmsf_workflow.name %>
            <% end %>
          </div>
          <div>
            <%= label_tag 'workflow_status', l(:field_status) %>
            <%= revision.workflow_str false %>                
          </div>            
        </div>        
        <div class="tab-content" id="tab-content-members">                     
          <table class="list">
            <thead><tr>
              <th></th>
              <th><%= l(:label_dmsf_workflow_step) %></th>
              <th><%= l(:label_user) %></th>
              <th><%= l(:label_action) %></th>
              <th><%= l(:link_workflow) %></th>
              <th><%= l(:label_note) %></th>
              <th><%= l(:label_date)%></th>              
            </tr></thead>
            <tbody>
            <tr id="step-0" class="even">
              <td class="id"></td>
                <td></td>
                <td></td>
                <td></td>
                <td><%= DmsfWorkflowStepAction.workflow_str(0) %></td>
                <td class="dmsf_note"></td>
                <td></td>
              </tr>
              <tr id="step-1" class="odd">
                <td class="id"></td>
                <td></td>
                <td><%= link_to_user revision.dmsf_workflow_assigned_by_user if revision.dmsf_workflow_assigned_by_user %></td>
                <td><%= DmsfWorkflowStepAction.action_str(DmsfWorkflowStepAction::ACTION_ASSIGN) %></td>
                <td><%= DmsfWorkflowStepAction.workflow_str(DmsfWorkflowStepAction::ACTION_ASSIGN) %></td>
                <td class="dmsf_note"></td>
                <td><%= format_time(revision.dmsf_workflow_assigned_at) if revision.dmsf_workflow_assigned_at %></td>
              </tr>  
              <tr id="step-2" class="even">
                <td class="id"></td>
                <td></td>
                <td><%= link_to_user revision.dmsf_workflow_started_by_user if revision.dmsf_workflow_started_by_user %></td>
                <td><%= DmsfWorkflowStepAction.action_str(DmsfWorkflowStepAction::ACTION_START) %></td>
                <td><%= DmsfWorkflowStepAction.workflow_str(DmsfWorkflowStepAction::ACTION_START) if revision.dmsf_workflow_started_by_user %></td>
                <td class="dmsf_note"></td>
                <td><%= format_time(revision.dmsf_workflow_started_at) if revision.dmsf_workflow_started_at %></td>
              </tr>
              <% sql = "SELECT c.action, c.note, c.created_at, c.author_id, a.user_id, s.step, s.name FROM dmsf_workflow_step_actions c RIGHT JOIN dmsf_workflow_step_assignments a ON a.id = c.dmsf_workflow_step_assignment_id RIGHT JOIN dmsf_workflow_steps s ON s.id = a.dmsf_workflow_step_id WHERE a.dmsf_file_revision_id = #{revision.id} ORDER BY s.step, s.id, c.action DESC, c.created_at" %>
              <% result = DmsfWorkflowStep.connection.exec_query sql %>
              <% last_step = 0 %>
              <% result.each_with_index do |row, i| %>
                <tr id="step-<%= i + 3 %>">
                  <td class="id"><%= row['step'] unless row['step'] == last_step %></td>
                  <% last_step = row['step'] %>
                  <td class="name"><%= row['name'] %></td>
                  <td><%= link_to_user User.find_by(id: row['author_id'].present? ? row['author_id'] : row['user_id']) %></td>
                  <td><%= DmsfWorkflowStepAction.action_str(row['action']) %></td>
                  <td>                    
                    <% if (row['step'].to_i == @dmsf_workflow.dmsf_workflow_steps.last.step) && (revision.workflow == DmsfWorkflow::STATE_APPROVED) && (row['action'] != DmsfWorkflowStepAction::ACTION_DELEGATE) %>
                      <%= l(:title_approved) %>
                    <% else %>
                      <%= DmsfWorkflowStepAction.workflow_str(row['action']) %>
                    <% end %>
                  </td>
                  <td class="dmsf_note"><%= row['note'] %></td>
                  <td><%= format_time(row['created_at']) if row['created_at'].present? %></td>
                </tr>
              <% end %>
            </tbody>
          </table>          
        </div>                               
      <% end %>
  <% end %>
</p>